Skip to content

Add Cost Estimation and Health Monitoring Example#1088

Open
haripriyarao26 wants to merge 11 commits into
google-gemini:mainfrom
haripriyarao26:feat-cost-observability
Open

Add Cost Estimation and Health Monitoring Example#1088
haripriyarao26 wants to merge 11 commits into
google-gemini:mainfrom
haripriyarao26:feat-cost-observability

Conversation

@haripriyarao26

Copy link
Copy Markdown

Fixes #1087

Key Features:

  1. Dynamic Pricing Engine: A reusable GeminiObservability class that calculates USD costs for 2025 Gemini models (2.0 Flash, 2.5 Pro, etc.).
  2. Resilient Health Checks: Implements logic to distinguish between system errors and 429 RESOURCE_EXHAUSTED quota limits.
  3. Local Usage Logging: Demonstrates how to persist usage metadata to JSON for internal budgeting and audit trails.
  4. Modern SDK: Built using the latest google-genai Python SDK.

@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@github-actions github-actions Bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Dec 30, 2025
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @haripriyarao26, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a comprehensive example for integrating cost estimation and health monitoring into Gemini API applications. It provides a GeminiObservability class that allows developers to track token usage, calculate real-time USD costs based on model-specific pricing, and perform robust health checks to ensure API availability and manage quota limits. This new example aims to empower users with better control and understanding of their Gemini API consumption, facilitating optimized spending and improved application resilience.

Highlights

  • New Example Notebook: Introduced a new Jupyter Notebook, Cost_Estimation_And_Health_Monitoring.ipynb, demonstrating how to implement cost observability and health checks for Gemini API applications.
  • GeminiObservability Class: Added a reusable Python class, GeminiObservability, designed to track token usage, estimate costs in USD for various Gemini models (e.g., 2.5 Flash, 2.5 Pro, 3-preview models), and perform API health checks.
  • Dynamic Pricing Engine: The GeminiObservability class includes a dynamic pricing engine with example 2025 rates for different Gemini models, allowing for real-time cost calculation based on input and output token counts.
  • API Health Checks: Implemented a check_health method within the GeminiObservability class to verify API availability and distinguish between general system errors and resource exhaustion (e.g., 429 quota limits).
  • Usage Logging and Summary: The notebook demonstrates how to extract usage metadata from API responses and generate a summary including model, token counts, and estimated cost, which can be used for internal budgeting and audit trails.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a valuable example notebook for cost estimation and health monitoring with the Gemini API. The GeminiObservability class is a well-structured and reusable component. I have a few suggestions to align the notebook with the repository's style guide and improve the code. Specifically, the new notebook should be added to examples/README.md, and its outputs should be saved as per the style guide. There are also opportunities to improve the code by adding missing type hints, using more specific exception handling, and avoiding redundant calculations.

Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
@haripriyarao26

Copy link
Copy Markdown
Author

@Giom-V @PaulTR

Just following up on this PR! I've implemented a Cost Observability & Health Utility that standardizes USD cost estimation and integration health checks directly from usage_metadata.

I’ve ensured the code is clean and follows the cookbook's standards. I'd love to get this merged to help other SDEs building cost-sensitive apps.

Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb
Comment thread examples/Cost_Estimation_And_Health_Monitoring.ipynb Outdated
@kkorpal

kkorpal commented May 19, 2026

Copy link
Copy Markdown
Collaborator

Hi @haripriyarao26 ! Thank you for putting together this Cost Estimation notebook; observability is a fantastic topic. I just tested it in Colab and ran into a few execution errors, including Markdown text accidentally formatted as code cells, outdated google-genai SDK token attributes, and missing exception imports causing a NameError. I've left inline comments with the exact fixes for all of these. Once you apply the fixes, please make sure to run all the cells so the notebook saves and displays the execution outputs before you push. Let me know when it's updated so I can take another look!

@kkorpal kkorpal added status:awaiting response Awaiting a response from the author and removed status:awaiting review PR awaiting review from a maintainer labels May 19, 2026
@haripriyarao26

Copy link
Copy Markdown
Author

Hi @kkorpal ,

Thank you so much! I am working on these changes and will soon update.

@haripriyarao26

Copy link
Copy Markdown
Author

Hi @kkorpal ,

I saw some issues related to the readme files that were blocking the GitHub Actions. I have resolved them.
Thanks

@kkorpal

kkorpal commented May 25, 2026

Copy link
Copy Markdown
Collaborator

Hi @haripriyarao26 ! Thanks for your contribution.
Before we can proceed with reviewing, it looks like a few automated checks are currently failing. Could you please address the following?

  1. Missing README Link: Our automated checks require all new notebooks to be linked in the main README.md file at the very root of the repository. Please add a bullet point linking to your notebook there.
  2. Notebook Format & Linting: The formatting and linting checks are also failing. Please click the 'Details' link next to the failing checks to see the exact errors. You can fix the formatting by running this locally:

python3 -m pip install -U --user git+https://github.com/tensorflow/docs
python3 -m tensorflow_docs.tools.nbfmt your_notebook_name.ipynb

Let us know once you've pushed the updates and the checks are green!"

@haripriyarao26

Copy link
Copy Markdown
Author

Hi @haripriyarao26 ! Thanks for your contribution. Before we can proceed with reviewing, it looks like a few automated checks are currently failing. Could you please address the following?

  1. Missing README Link: Our automated checks require all new notebooks to be linked in the main README.md file at the very root of the repository. Please add a bullet point linking to your notebook there.
  2. Notebook Format & Linting: The formatting and linting checks are also failing. Please click the 'Details' link next to the failing checks to see the exact errors. You can fix the formatting by running this locally:

python3 -m pip install -U --user git+https://github.com/tensorflow/docs python3 -m tensorflow_docs.tools.nbfmt your_notebook_name.ipynb

Let us know once you've pushed the updates and the checks are green!"

Hi @kkorpal ,
Working on this

@haripriyarao26 haripriyarao26 force-pushed the feat-cost-observability branch from acece2e to 2943bf5 Compare June 7, 2026 06:48
@haripriyarao26

Copy link
Copy Markdown
Author

Hi @kkorpal ,

I saw some issues related to the Readme/Notebook file. I have resolved them.
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component:examples Issues/PR referencing examples folder status:awaiting response Awaiting a response from the author type:feature request New feature request/enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding a new example - Token Usage Tracking with Health Monitoring Utility

2 participants